package myQueue;

import java.util.Stack;

public class Test3 {
    public static void main(String[] args) {
        MinStack stack=new MinStack();
        stack.push(4);
        stack.push(-2);
        stack.push(0);
        stack.push(2);
        stack.push(-1);
        stack.push(-2);

        System.out.println(stack.getMin());
        System.out.println(stack.top());
        System.out.println("---------");


    }
}
class MinStack {
    //stack1用于存放所有添加的元素
    //minStack存放的是最小元素
    Stack<Integer> stack1;
    Stack<Integer> minStack;

    public MinStack() {
        stack1=new Stack<>();
        minStack=new Stack<>();
    }

    public void push(int val) {
        //判断是否是第一次插入
        if(stack1.empty()&&minStack.empty()){
            stack1.push(val);
            minStack.push(val);
        }else{
            //新添加的元素和最小栈中的第一个最小元素进行比较
            //如果比其更小的话，则添加到stack1和最小栈中，反之则只添加到stack1
            int temp=minStack.peek();
            if(val<=temp){
                minStack.push(val);
                stack1.push(val);
            }else{
                stack1.push(val);
            }

        }
    }

    public void pop() {
        if(stack1.empty()){
            return;
        }
        int temp=stack1.pop();
        if(temp==minStack.peek()){
            minStack.pop();
        }
    }

    public int top() {
        return stack1.peek();
    }

    public int getMin() {
        return minStack.peek();
    }
}